﻿namespace 结构体
{
    // 定义一个结构体，需要放在命名空间中，否则会报错

    struct Point
    {
        public int X;
        public int Y;

        public Point(int x, int y)
        {
            X = x;
            Y = y;
        }

        public void Display()
        {
            Console.WriteLine($"X: {X}, Y: {Y}");
        }

        public void Move(int dx, int dy)
        {
            X += dx;
            Y += dy;
        }

        public override string ToString()
        {
            return $"Point(X: {X}, Y: {Y})";
        }

        public override int GetHashCode()
        {
            return X.GetHashCode() ^ Y.GetHashCode();
        }

        public override bool Equals(object obj)
        {
            if (obj is Point other)
            {
                return X == other.X && Y == other.Y;
            }
            return false;
        }

        public static bool operator ==(Point a, Point b)
        {
            return a.Equals(b);
        }

        public static bool operator !=(Point a, Point b)
        {
            return !a.Equals(b);
        }

        public static Point operator +(Point a, Point b)
        {
            return new Point(a.X + b.X, a.Y + b.Y);
        }

        public static Point operator -(Point a, Point b)
        {
            return new Point(a.X - b.X, a.Y - b.Y);
        }
        public static Point operator *(Point a, int scalar)
        {
            return new Point(a.X * scalar, a.Y * scalar);
        }

        public static Point operator /(Point a, int scalar)
        {
            if (scalar == 0)
                throw new DivideByZeroException("Cannot divide by zero.");
            return new Point(a.X / scalar, a.Y / scalar);
        }

        
        public static implicit operator Point((int x, int y) tuple)
        {
            return new Point(tuple.x, tuple.y);
        }

        public static explicit operator (int x, int y)(Point point)
        {
            return (point.X, point.Y);
        }


        public static implicit operator Point(int value)
        {
            return new Point(value, value);
        }

    }


    // 定义一个人员结构体，可以在整个命名空间中使用
    struct Person
    {
        public string Name;
        public int Age;
        public string Address;

        // 构造函数
        public Person(string name, int age, string address)
        {
            Name = name;
            Age = age;
            Address = address;
        }

        // 方法
        public void DisplayInfo()
        {
            Console.WriteLine($"Name: {Name}, Age: {Age}, Address: {Address}");
        }
    }

    internal class Program
    {

        // 定义一个表示列表的结构体，这个只能在这个类中使用
        public struct List<T>
        {
            public T[] Items;
            public int Count;
            public void Add(T item)
            {
                if (Count == Items.Length)
                {
                    Array.Resize(ref Items, Count * 2);
                }
                Items[Count++] = item;
            }
        }

        static void Main(string[] args)
        {
            Console.WriteLine("Hello, World!");

            Point p = new()
            {
                X = 10,
                Y = 20
            };
            Console.WriteLine($"X:{p.X}, Y:{p.Y}");

            p += new Point(5, 5);

            p.Display();

            p.Move(10, 10);
            p.Display();

            if (p == new Point(15, 25))
            {
                Console.WriteLine("p is equal to (15, 25)");
            }
            else
            {
                Console.WriteLine("p is not equal to (15, 25)");
            }

                // 使用人员结构体
                Person person = new()
            {
                Name = "John Doe",
                Age = 30,
                Address = "123 Main St"
            };
        }
    }
}
